<?php

namespace app\admin\controller\mall;

use app\admin\model\MallGoods;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\model\MallCate;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="商品销售")
 */
class Database extends AdminController
{
    use Curd;
    protected $relationSearch = true;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallGoods();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function goods()
    {
        $manager_id =$this->request->param('id',0,'intval');
        $type =$this->request->param('type',0,'intval');
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            if($manager_id > 0){
                $where[] =[['manager_id','=',$manager_id]];
            }
            $count = $this->model
                ->withJoin('cate', 'LEFT')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin('cate', 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('manager_id',$manager_id);
        $this->assign('type',$type);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="导出")
     */
    public function export()
    {
        $where=[];
        $ids =$this->request->param('id','','trim');
        $where[] =[['id','in',$ids]];

        $tableName = $this->model->getName();
        $tableName = CommonTool::humpToLine(lcfirst($tableName));
        $prefix = config('database.connections.mysql.prefix');
        $dbList = Db::query("show full columns from {$prefix}{$tableName}");
        $header = [];
        foreach ($dbList as $vo) {
            $comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
            if (!in_array($vo['Field'], ['manager_id','doors','short_title','images','tags','market_price','cost_price','specifications','sku_id','buy_times','hx_times','content','buydesc','sort','isindex','islimit','limitnum','perlimitnum','status','remark','update_time','delete_time'])) {
                $header[] = [$comment, $vo['Field']];
            }
        }
        $list = $this->model
            ->where($where)
            ->order('id', 'desc')
            ->select()
            ->toArray();
        foreach ($list as $k=>$v){
            $cat =(new MallCate())->find($v['cate_id']);
            $list[$k]['cate_id'] =!empty($cat)?$cat['title']:'';
        }
        $fileName = time();
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }


}